package com.byq.mm.abstractx.engine;


import com.byq.mm.abstractx.device.Device;
import com.byq.mm.abstractx.protocol.Defination;

public class DefinationProcessor {
	CoreEnvironment env;
	public DefinationProcessor(CoreEnvironment env) {
		super();
		this.env = env;
	}
	public void process(Defination def, String from){
		Device d = def.getDevice();
		if (d != null)
			d.setTansportId(from);
		if (def.isOffer()) {
			if (env.getDefinationConfig().forOffer().equals("accept")){
				env.getClientDeviceManager().accept(def);
				env.getEventCenter().notifyOfferAccepted(def, from);
			}
			
			else if (env.getDefinationConfig().forOffer().equals("reject")) {
				env.getClientDeviceManager().reject(def);
				env.getEventCenter().notifyOfferRejected(def, from);
			} else {
				
				env.getEventCenter().notifyInvite(def);
			}
		}
		if (def.isAccept()) {
			env.getServerDeviceManager().accepted(from);
			if (def.getDevice() != null)
				env.getClientDeviceManager().accept(def);
			env.getEventCenter().notifyAccept(from);
		}
		if (def.isReject()) {
			env.getEventCenter().notifyRefuse(from);
		}

		if (def.isRequest()) {
			if (env.getDefinationConfig().forRequest().equals("agree")){
				env.getServerDeviceManager().offer(from);
				env.getEventCenter().notifyRequestAgree(def, from);
			}
			else if (env.getDefinationConfig().forOffer().equals("refuse")){ {
				env.getClientDeviceManager().refuse(from);
				env.getEventCenter().notifyRequestRefuse(def, from);
			}
			} else {

				env.getEventCenter().notifyRequest(def, from);
			}
		}
		if (def.isRefuse()) {

		}
	}
}
